﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TJADRICD.CollaborativeDesignPlugin.Commission;

namespace TJADRICD.CollaborativeDesignPlugin.Form
{
    public partial class FormCommission : System.Windows.Forms.Form
    {
        private String commissionUserId;
        private String commissionUserName;
        private CommandSignCommission signCommissionFunction = null;
        List<SignCommissionUser> commissionedList = null;
        public FormCommission(String userId, String userName)
        {
            commissionUserId = userId;
            commissionUserName = userName;
            commissionedList = new List<SignCommissionUser>();
            signCommissionFunction = new CommandSignCommission();
            InitializeComponent();
            
        }



        private void CancelBt_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void FormCommission_Load(object sender, EventArgs e)
        {
            ExceptionInfLabel.Text = "";
            CommissionNameLabel.Text = commissionUserName;
            commissionedList = signCommissionFunction.GetCommissionedList(commissionUserId);
            #region 调试数据
            SignCommissionUser c1 = new SignCommissionUser();
            SignCommissionUser c2 = new SignCommissionUser();
            c1.userId = "11";
            c1.userName = "11Name";
            c2.userId = "22";
            c2.userName = "22Name";
            commissionedList.Add(c1);
            commissionedList.Add(c2);
            #endregion

            foreach (SignCommissionUser userItem in commissionedList)
            {
                CommissionedNameBox.Items.Add(userItem.userName);
                userItem.ComboxNameIndex = CommissionedNameBox.Items.Count - 1;
            }

            
        
        }
        
        private bool FindCommissionedNameBoxUserName(String commissionedName,ref SignCommissionUser commissionedUser)
        {
            commissionedUser = commissionedList.Find(delegate(SignCommissionUser user) { return user.userName == commissionedName; });
            if (commissionedUser != null)
                return true;
            return false;
        }

        private void FindCommissionNameBt_Click(object sender, EventArgs e)
        {
            SignCommissionUser commissionedUser = null;
            bool isOriginalNameListExist = false;
            if (OtherCommissionNameBox.Text != "")
            {
                String commissionedUserName = OtherCommissionNameBox.Text;
                isOriginalNameListExist = FindCommissionedNameBoxUserName(commissionedUserName, ref commissionedUser);
                if (!isOriginalNameListExist)
                {
                    commissionedUser = signCommissionFunction.FindCommissionedUser(commissionedUserName);
                }
            }

            if (commissionedUser != null)
            {
                if (!isOriginalNameListExist)
                {
                    
                    CommissionedNameBox.Items.Add(commissionedUser.userName);
                    commissionedUser.ComboxNameIndex = CommissionedNameBox.Items.Count - 1;
                    commissionedList.Add(commissionedUser);
                }
                CommissionedNameBox.SelectedIndex = commissionedUser.ComboxNameIndex;
                ExceptionInfLabel.Text = "查找成功";
            }
            else
                ExceptionInfLabel.Text = "未找到被委托人";
        }

        private void ConfirmBt_Click(object sender, EventArgs e)
        {
            DBCommissionRecord commissionRecord = new DBCommissionRecord();
            SignCommissionUser commissionedUser = new SignCommissionUser();
            if (this.CommissionedNameBox.SelectedItem != null)
                commissionedUser = commissionedList.Find(delegate(SignCommissionUser user) { return user.ComboxNameIndex == this.CommissionedNameBox.SelectedIndex; });

            commissionRecord.commissionId = commissionUserId;
            commissionRecord.commissionedId = commissionedUser.userId;
            commissionRecord.startDate = this.StartTimePicker.Value;
            commissionRecord.finishDate = this.DeadlinePicker.Value;

            if (commissionRecord.startDate > commissionRecord.finishDate)
            {
                MessageBox.Show("起始日期必须小于终止日期");

            }
            else if (commissionRecord.commissionedId == null)
            {
                MessageBox.Show("请选择被委托人");
            }
            else
            {
                signCommissionFunction.AddCommissionRecord(commissionRecord);
                this.Close();
            }
        }

        private void CommissionedNameBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



    }
}
